#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>
#include "../fh_test.h"



int global_pid; 
int data[DATA_SIZE] __attribute__ ((aligned(16)));

int Run(){
	int pid = csp_pid();

	global_pid = csp_pid();

	int n, interval_begin=0, interval_end=0;
	int interval[2] __attribute__ ((aligned(16)));
	interval[0] = -1;
	interval[1] = -1;


	printf("Verifier started with pid %i.\n", pid);

	csp_channel init2verifier = 
		csp_create_channel(ONE2ONE_CHANNEL, INIT2VERIFIER, sizeof(int)*2);
	csp_channel_read(init2verifier, interval);

	csp_channel harvester2verifier = 
		csp_create_channel(ONE2ONE_CHANNEL, HARVESTER2VERIFIER, sizeof(int)*DATA_SIZE);

	csp_channel_read(harvester2verifier,data);

	/* Verify data. */
	for(n=0; n < DATA_SIZE; n++){
		if(data[n] && !interval_begin){
			interval_begin = n;
		}
		if(!data[n] && interval_begin && !interval_end){
			interval_end = n;
		}
	}

	printf("Found interval (%i, %i).\n", interval_begin, interval_end);

	ASSERT( interval[0] == interval_begin);
	ASSERT( interval[1] == interval_end);


	printf("Exiting network\n");


	ASSERT( pid == csp_pid() );
	ASSERT( global_pid == csp_pid() );

	PRINTF_FOR_TESTING("%s terminating on spe #%i with pid %i\n",__FILE__ , csp_spe_id(), pid);

	PRINTF_FOR_TESTING("Nested task with pid=%i terminated with res=%i.\n", pid, res);

	csp_exit_network();
	return 0;
}
